<?php
/**
 * Created by PhpStorm.
 * User: 84071
 * Date: 2018-10-04
 * Time: 11:57
 */
namespace app\api\controller;

use app\common\controller\Apibase;
use app\common\model\Order as cxModel;
use app\common\model\OrderCont;
use app\common\model\OrderShop;

class Order extends Apibase{
    /**
     * 插入订单
     * 本方法仅接收POST方法 必选值 UID SHOP[AID NUM] CLASS TIME
     * @return \think\response\Json
     */
    public function addOrder(){
        $res['code'] = '0';
        $getdata = request()->param();
        $getdata = datatrim($getdata);
        if(empty($getdata['uid'])){
            $res['code'] = '2';
            $res['msg'] = '您还没有登录，请重新登录';
            return json($res);
        }
        $shop = $getdata['shop'];
        foreach ($shop as $v){
            if(empty($v['aid'])){
                $res['msg'] = '请选择产品';
                return json($res);
            }
            if(empty($v['num'])){
                $res['msg'] = '请选择产品数量';
                return json($res);
            }
        }
        if(empty($getdata['class'])){
            $getdata['class'] = '1';
        }
        $cxmodel = new cxModel();
        $getlist = $cxmodel->add_order($getdata);
        return json($getlist);
    }
    /**
     * 订单查询
     */
    public function getOrder(){
        $getdata = request()->param();
        $getdata = datatrim($getdata);
        $cxmodel = new cxModel();
        $reslist = $cxmodel->get_list($getdata);
        $res['code'] = '0';
        $res['msg'] = '查询成功';
        $res['cont'] = $reslist;
        return json($res);
    }
    /**
     * 添加购物车
     * @return array|mixed|\think\response\Json
     */
    public function addOrderShop(){
        $getdata = request()->param();
        $getdata = datatrim($getdata);
        $res['code'] = '0';
        if(empty($getdata['uid'])){
            $res['msg'] = '您还没有登录，请登录';
            return json($res);
        }
        if(empty($getdata['aid'])){
            $res['msg'] = '请选择产品';
            return json($res);
        }
        $shopmodel = new OrderShop();
        if($shopmodel->addOne($getdata)){
            $res['code'] = '1';
            $res['msg'] = '添加购物车成功';
        }else{
            $res['msg'] = '添加购物车失败';
        }
        return json($res);
    }
    //  删除购物车商品
    public function Orderdelche(){
        $res['code'] = '0';
        $getdata = request()->param();
        $getdata = datatrim($getdata);
        if(empty($getdata['del'])){
            $res['msg'] = '请选择要删除的商品';
            return json($res);
        }
        $shopmodel = new OrderShop();
        if($shopmodel->delList($getdata)){
            $res['code'] = '1';
            $res['msg'] = '处理成功';
        }else{
            $res['msg'] = '处理失败';
        }
        return json($res);
    }
    //  查询产品购买量
    public function getArticle(){
        $res['code'] = '0';
        $getdata = request()->param();
        $getdata = datatrim($getdata);
        $cxmodel = new OrderCont();
        if(empty($getdata['aid']) && empty($getdata['mid'])){
            $res['msg'] = '请输入要查询内容';
            return json($res);
        }
        if(!empty($getdata['aid'])){
            $map['aid'] = $getdata['aid'];
        }
        if(!empty($getdata['mid'])){
            $map['mid'] = $getdata['mid'];
        }
        $res['code'] = '1';
        $res['nums'] = $cxmodel->where($map)->sum('num');
        $res['msg'] = '查询成功';
        return json($res);
    }

}